/*
 * @Description:
 * @Version: 1.0
 * @Author: wenbin
 * @Date: 2021-09-15 11:10:41
 * @LastEditors: wenbin
 * @LastEditTime: 2021-11-08 14:56:25
 * @FilePath: /vite-ts-vue3-test/src/router/routerFilter.ts
 * Copyright (C) 2021 wenbin. All rights reserved.
 */
import router from './index';
import Cookie from 'js-cookie';
import NProgress from 'nprogress';

const whiteList = ['/login', '/license', '/test'];

// if (auth_token) {
//   localStorage.setItem('token', auth_token);
// }

router.beforeEach((to, from, next) => {
  const auth_token: string | undefined = Cookie.get('auth_token');
  NProgress.start();
  if (auth_token) {
    if (!localStorage.getItem('token')) {
      localStorage.setItem('token', auth_token);
    }
    if (to.path === '/login') {
      next({
        path: '/',
      });
    } else {
      next();
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next('/login');
    }
  }
});

router.afterEach(() => {
  NProgress.done();
});
